<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Book Detail</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/index.css">
    <script src="js/jquery-3.3.1.min.js"></script>
</head>
<body>
    <div class="container-fluid">
        <!-- header-keyword -->
        <div class="row" id="header">
            <div class="col-md-10">
                <input type="text" class="form-control" id="keyword" placeholder="keyword">
            </div>
            <div class="col-md-2">
                <button type="button" class="btn btn-info">Search</button>
            </div>
        </div>
        <!-- nav -->
        <div class="row">
            <div class="col-md-6"></div>
            <div class="col-md-6" align="right" >
                <button type="button" id="btn_login" class="btn btn-link btn-sm">Sign in</button>
                <button type="button" id="btn_register" class="btn btn-link btn-sm">Register</button>
                <button type="button" id="btn_logout" class="btn btn-link btn-sm">Logout</button>
                <button class="btn btn-info btn-sm" id="btn_cart" type="button">
                    Cart <span class="badge" id="cartnum">0</span>
                </button>
            </div>
        </div>

        <!-- main -->
        <div class="row" id="main">
            <div class="col-md-12">
                <!-- bookDetail -->
                <div id="bookDetail">
                    <div class="col-md-12">
                        <button type="button" class="btn btn-link btn-sm" id="home">Home</button>
                        >
                        <button type="button" id="nav_bookname" class="btn btn-link btn-sm">The Creature Choir</button>
                        <div id="bookdetails">
                            <!-- <h3>The Creature Choir</h3>
                            <img src="images/book_1.jpeg" width="" alt="" srcset="">
                            <div class="detail">
                                <p>Author: Word Willms </p>
                                <p>Published: HarperCollins Publishers</p>
                                <p>Publisher: HarperCollins Publishers</p>
                                <p>Category: Storybook</p>
                                <p>Language: English</p>
                                <p>Description: Sign Your heart out with a whole  choir of characters.</p>
                                <p>Price: $117</p>
                            </div> -->
                        </div>

                        <div class="col-md-12" style="padding-left:0;margin-bottom: 2rem;">
                            <span style="font-weight: bold;">Order:</span>
                            <input type="number" id="count"  placeholder="Count" style="width:200px;" value="1">
                            <button type="button" class="btn btn-info btn-sm" id="btn_add">Add to Cart</button>
                        </div>

                    </div>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function(){
            //API URL
            var apiurl="http://localhost:8080";
            var bookinfo={};

            function islogin(){
                var currentuser=JSON.parse(sessionStorage.getItem("book_user"));
                console.log(currentuser);
                if(currentuser==undefined || currentuser==null){
                    $("#btn_login").show();
                    $("#btn_register").show();
                    $("#btn_logout").hide();
                }else{
                    $("#btn_login").hide();
                    $("#btn_register").hide();
                    $("#btn_logout").show();
                }
            }

            islogin();

            get_Book();

            //Book
            function get_Book(){
                var bookid=getUrlParam("id");
                var params={book_id:bookid};
                $.ajax({
                    url: apiurl+"/Books.php",
                    type: "POST",
                    dataType: "json",
                    data:params,
                    success: function (data) {
                        console.log(data);
                        if(data.code==0){
                            var book=data.data.books[0];
                            bookinfo=book;
                            $("#nav_bookname").text(book.name);
                            var el_book=$("#bookdetails");
                            var html='<h3>'+book.name+'</h3>'+
                            '<img src="'+book.img+'" width="" alt="" srcset="">'+
                            '<div class="detail">'+
                            '<p>Author: '+book.author+' </p>'+
                            '<p>Published: '+book.published+'</p>'+
                            '<p>Publisher: '+book.publishr+'</p>'+
                            '<p>Category: '+book.category+'</p>'+
                            '<p>Language: '+book.lang+'</p>'+
                            '<p>Description: '+book.description+'</p>'+
                            '<p>Price: $'+book.price+'</p>'+
                            '</div>';
                            el_book.html(html);
                        }else{
                            alert(data.msg);
                        }
                    }
                });


                var book_cart=sessionStorage.getItem("book_cart");
                if(book_cart==null || book_cart==undefined){
                    $("#cartnum").text(0);
                }else{
                    book_cart=JSON.parse(book_cart);
                    $("#cartnum").text(book_cart.length);
                }

            }

            //Add cart
            $("#btn_add").click(function(){
                var num=$("#count").val();
                if(num<1){
                    alert("The number is at least one");
                    return;
                }
                var book_cart=sessionStorage.getItem("book_cart");
                if(book_cart==null || book_cart==undefined){
                    book_cart=[];
                    bookinfo.Quantity=(num*1);
                    book_cart.push(bookinfo);
                }else{
                    var flag=false;
                    book_cart=JSON.parse(book_cart);
                    for(var i=0;i<book_cart.length;i++){
                        if(book_cart[i].id==bookinfo.id){
                            book_cart[i].Quantity=(book_cart[i].Quantity*1)+(num*1);
                            flag=true;
                            break;
                        }
                    }
                    if(!flag){
                        bookinfo.Quantity=(num*1);
                        book_cart.push(bookinfo);
                    }
                }
                sessionStorage.setItem("book_cart",JSON.stringify(book_cart));
                window.location.href="mycart.html";

            });

            //back home
            $("#home").click(function(){
                window.location.href="index.html";
            });

             //Login
             $("#btn_login").click(function(){
                window.location.href="login.html";
            });
            //Register
            $("#btn_register").click(function(){
                window.location.href="createaccount.html";
            });
            //Logout
            $("#btn_logout").click(function(){
                $.ajax({
                    url: apiurl+"/Logout.php",
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        sessionStorage.removeItem("book_user");
                        $($("body")[0]).html('<h2 style="text-align:center;color:red;">Logging out</h2>');
                        setInterval(() => {
                            window.location.href="index.html";
                        }, 3000);
                    }
                });
            });

            $("#btn_cart").click(function(){
                window.location.href="mycart.html";
            });

            //get params
            function getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]); return null;
            }

        })
    </script>
</body>
</html>